package fr.cephb.joperon.swing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenu;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;


import fr.cephb.joperon.core.ProjectAcnSuffix;
import fr.cephb.joperon.core.db.PrjDnaDB;
import fr.cephb.joperon.core.db.PrjFinDB;
import fr.cephb.joperon.core.entities.PrjDna;
import fr.cephb.joperon.core.entities.PrjFin;
import fr.cephb.joperon.core.entities.PrjMgr;
import fr.cephb.swing.GenericTableModel;

/**
 * PrjFinFrame
 * @author lindenb
 *
 */
public class PrjFinFrame extends  OperonFrame
	{
	private static final long serialVersionUID = 1L;

	private static class Model
		extends GenericTableModel<PrjFin>
		{
		private static final long serialVersionUID = 1L;
		@Override
		public int getColumnCount() {
			return 5;
			}
		
		@Override
		public String getColumnName(int columnIndex) {
			switch(columnIndex)
				{
				case 0: return "Project-ID";
				case 1: return "Date";
				case 2: return "Author";
				case 3: return "Description";
				case 4: return "Fix";
				}
			return null;
			}
		
		@Override
		public Object getValueOf(PrjFin item, int columnIndex) {
			switch(columnIndex)
				{
				case 0: return item.getPid();
				case 1: return item.getYmd();
				case 2: return item.getLog();
				case 3: return item.getDef();
				case 4: return item.getFix();
				}
			return null;
			}
		}

	private Model tableModel;
	private JTable table;
	private PrjMgr prjMgr;
	public PrjFinFrame(OperonGUI operon,PrjMgr prjMgr)
		{
		super(operon,"Project:" +prjMgr.getPid());
		this.prjMgr=prjMgr;
		AbstractAction action= new AbstractAction("Projects Inputs")
			{
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e)
				{
				select();
				}
			};

		JMenu menu= new JMenu("Action");
		menu.add(action);
		getJMenuBar().add(menu);
		
		this.tableModel= new Model();
		this.table= new JTable(this.tableModel);
		
		super.content.add(new JScrollPane(this.table),BorderLayout.CENTER);
		
		this.addInternalFrameListener(new InternalFrameAdapter()
			{
			@Override
			public void internalFrameOpened(InternalFrameEvent e) {
				reloadContent();
				}
			});
		
		this.table.addMouseListener(new MouseAdapter()
			{
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount()<2) return;
				select();
				}
			});
		
		selectionListen(action,this.table);
		}
	
	public PrjMgr getPrjMgr() {
		return prjMgr;
		}
	
	private void select()
		{
		int i=table.getSelectedRow();
		i= table.convertRowIndexToModel(i);
		if(i==-1) return;
		PrjFin object= tableModel.elementAt(i);
		ProjectAcnSuffix suffix=null;//object.();
		if(suffix==null) return;
		
		switch(suffix)
			{
			case D:
				{
				PrjDnaDB db=null;
				try {
					db= new PrjDnaDB(getOperon());
					db.open();
					PrjDna dna=db.get(object.getPid()+"_"+object.getFix());
					db.close();
					if(dna!=null)
						{
						PrjDnaFrame f= new PrjDnaFrame(getOperonGUI(),dna);
						getDesktopPane().add(f);
						f.setVisible(true);
						}
					}
				catch (Exception e) {
					JOptionPane.showMessageDialog(
							getOperonGUI(),
							e.getMessage()
							);
					}
				finally
					{
					safeClose(db);
					}
				break;
				}
			}
		}
	

	public void reloadContent()
		{
		PrjFinDB db=null;
		try {
			db = new PrjFinDB(getOperon());
			db.open();
			this.tableModel.removeAll();
			this.tableModel.addAll(db.get(getPrjMgr().getPid()));
			} 
		catch (Exception e)
			{
			e.printStackTrace();
			}
		finally
			{
			safeClose(db);
			}
		
		}
	
	}
